package com.granada.behaviour.base;

import com.alibaba.druid.util.StringUtils;
import com.granada.entity.SysPermission;
import com.granada.exception.BusinessException;
import com.granada.exception.code.BaseResponseCode;
import com.granada.utils.MapperBeanUtils;
import com.granada.vo.resp.PermissionRespNode;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public abstract class PermissionBehavior  implements com.granada.behaviour.interfaces.PermissionBehavior {
    private void verifyForm(SysPermission sysPermission){
        verifyFormPid(sysPermission);
        /**
         * id 不为空说明是编辑
         */
        if(sysPermission.getId() > 0L) {
            List<SysPermission> list = MapperBeanUtils.getSysPermissionMapper().selectChild(sysPermission.getId());
            if (!list.isEmpty()) {
                throw new BusinessException(BaseResponseCode.OPERATION_MENU_PERMISSION_UPDATE);
            }
        }

    }
    private void verifyFormPid(SysPermission sysPermission){
        SysPermission parent=MapperBeanUtils.getSysPermissionMapper().selectByPrimaryKey(sysPermission.getPid());
        switch (sysPermission.getType()){
            case 1:
                if(parent!=null){
                    if(parent.getType()!=1){
                        throw new BusinessException(BaseResponseCode.OPERATION_MENU_PERMISSION_CATALOG_ERROR);
                    }
                }else if(!sysPermission.getPid().equals("0")){
                    throw new BusinessException(BaseResponseCode.OPERATION_MENU_PERMISSION_CATALOG_ERROR);
                }
                break;
            case 2:
//                if(parent==null||parent.getType()!=1){
//                    throw new BusinessException(BaseResponseCode.OPERATION_MENU_PERMISSION_MENU_ERROR);
//                }
                if(StringUtils.isEmpty(sysPermission.getUrl())){
                    throw new BusinessException(BaseResponseCode.OPERATION_MENU_PERMISSION_URL_NOT_NULL);
                }

                break;
            case 3:
                if(parent==null||parent.getType()!=2){
                    throw new BusinessException(BaseResponseCode.OPERATION_MENU_PERMISSION_BTN_ERROR);
                }
                if(StringUtils.isEmpty(sysPermission.getPerms())){
                    throw new BusinessException(BaseResponseCode.OPERATION_MENU_PERMISSION_URL_PERMS_NULL);
                }
                if(StringUtils.isEmpty(sysPermission.getUrl())){
                    throw new BusinessException(BaseResponseCode.OPERATION_MENU_PERMISSION_URL_NOT_NULL);
                }
                if(StringUtils.isEmpty(sysPermission.getMethod())){
                    throw new BusinessException(BaseResponseCode.OPERATION_MENU_PERMISSION_URL_METHOD_NULL);
                }
                break;
        }
    }

    protected List<PermissionRespNode> getTree(List<SysPermission> all, boolean type){

        List<PermissionRespNode> list=new ArrayList<>();
        if (all==null||all.isEmpty()){
            return list;
        }
        for(SysPermission sysPermission:all){
            if(sysPermission.getPid().equals(0L)){
                PermissionRespNode permissionRespNode = PermissionRespNode.builder().build();
                BeanUtils.copyProperties(sysPermission,permissionRespNode);
                permissionRespNode.setTitle(sysPermission.getName());

                if(type){
                    permissionRespNode.setChildren(getChildExcBtn(sysPermission.getId(),all));
                }else {
                    permissionRespNode.setChildren(getChildAll(sysPermission.getId(),all));
                }
                list.add(permissionRespNode);
            }
        }
        return list;
    }

    protected List<PermissionRespNode> getTreePlatform(List<SysPermission> all, boolean type){
        List<PermissionRespNode> list=new ArrayList<>();
        if (all==null||all.isEmpty()){
            return list;
        }
        /**
         * 权限管理(厂商端)
         */
        for(SysPermission sysPermission:all){
            if( sysPermission.getPid().equals("0")){
                PermissionRespNode permissionRespNode = PermissionRespNode.builder().build();
                BeanUtils.copyProperties(sysPermission,permissionRespNode);
                permissionRespNode.setTitle(sysPermission.getName());

                if(type){
                    permissionRespNode.setChildren(getChildExcBtn(sysPermission.getId(),all));
                }else {
                    permissionRespNode.setChildren(getChildAll(sysPermission.getId(),all));
                }
                list.add(permissionRespNode);
            }
        }
        return list;
    }

    protected List<PermissionRespNode> getTreePlatformAll(List<SysPermission> all,boolean type){

        List<PermissionRespNode> list=new ArrayList<>();
        if (all==null||all.isEmpty()){
            return list;
        }
        /**
         * 权限管理(厂商端)
         */
        for(SysPermission sysPermission:all){
            if(sysPermission.getPid().equals(0L)){
                PermissionRespNode permissionRespNode = PermissionRespNode.builder().build();
                BeanUtils.copyProperties(sysPermission,permissionRespNode);
                permissionRespNode.setTitle(sysPermission.getName());

                if(type){
                    permissionRespNode.setChildren(getChildExcBtn(sysPermission.getId(),all));
                }else {
                    permissionRespNode.setChildren(getChildAll(sysPermission.getId(),all));
                }
                list.add(permissionRespNode);
            }
        }
        return list;
    }

    protected List<PermissionRespNode>getChildExcBtn(Long id,List<SysPermission> all){

        List<PermissionRespNode> list=new ArrayList<>();
        for(SysPermission sysPermission:all){
            if(sysPermission.getPid().equals(id)&&sysPermission.getType()!=3){
                PermissionRespNode permissionRespNode = PermissionRespNode.builder().build();
                BeanUtils.copyProperties(sysPermission,permissionRespNode);
                permissionRespNode.setTitle(sysPermission.getName());
                permissionRespNode.setChildren(getChildExcBtn(sysPermission.getId(),all));
                list.add(permissionRespNode);
            }
        }
        return list;
    }

    protected List<PermissionRespNode>getChildAll(Long id,List<SysPermission> all){

        List<PermissionRespNode> list=new ArrayList<>();
        for(SysPermission sysPermission:all){
            if(sysPermission.getPid().equals(id)){
                PermissionRespNode permissionRespNode = PermissionRespNode.builder().build();
                BeanUtils.copyProperties(sysPermission,permissionRespNode);
                permissionRespNode.setTitle(sysPermission.getName());
                permissionRespNode.setChildren(getChildAll(sysPermission.getId(),all));
                list.add(permissionRespNode);
            }
        }
        return list;
    }
    public List<SysPermission> getPermission(Long userId) {
        List<Long> roleIds = MapperBeanUtils.getSysUserRoleMapper().getRoleIdsByUserId(userId);
        if(roleIds.isEmpty()){
            return null;
        }
        List<Long> permissionIds= MapperBeanUtils.getSysRolePermissionMapper().getPermissionIdsByRoles(roleIds);
        if (permissionIds.isEmpty()){
            return null;
        }
        List<SysPermission> result=MapperBeanUtils.getSysPermissionMapper().selectInfoByIds(permissionIds);
        return result;
    }
    private List<SysPermission> selectAll() throws Exception {
        List<SysPermission> result =MapperBeanUtils.getSysPermissionMapper().selectAll();
        if(!result.isEmpty()){
            for (SysPermission sysPermission:result){
                SysPermission parent=MapperBeanUtils.getSysPermissionMapper().selectByPrimaryKey(sysPermission.getPid());
                if (parent!=null){
                    sysPermission.setPidName(parent.getName());
                }
            }
        }
        return result;
    }

    /**
     * 开发端
     * @param userId
     * @return
     */
    public List<PermissionRespNode> permissionTreeList(Long userId) {
        List<SysPermission> list= getPermission(userId);
        return getTree(list,true);
    }

    /**
     * 厂商端的权限控制
     * @return
     */
    public List<SysPermission> selectAllPlatform(Long userId) {
        //獲取所有的權限
        List<SysPermission> result =MapperBeanUtils.getSysPermissionMapper().selectAllPlatform();
        if(!result.isEmpty()){
            for (SysPermission sysPermission:result){
                SysPermission parent=MapperBeanUtils.getSysPermissionMapper().selectByPrimaryKeyPlatform(sysPermission.getPid());
                if (parent!=null){
                    sysPermission.setPidName(parent.getName());
                }
            }
        }
        return result;
    }

    /**
     * 开发端
     * @return
     */
    public List<PermissionRespNode> selectBig() throws Exception {
        List<SysPermission> list=selectAll();
        return getTree(list,false);
    }

    public List<PermissionRespNode> selectAllMenuByTree(Long permissionId) throws Exception {
        List<SysPermission> list = selectAll();
        if(!list.isEmpty() && permissionId != null){
            for (SysPermission sysPermission:list){
                if (sysPermission.getId().equals(permissionId)){
                    list.remove(sysPermission);
                    break;
                }
            }
        }
        List<PermissionRespNode> result=new ArrayList<>();
        //新增顶级目录是为了方便添加一级目录
        PermissionRespNode respNode = PermissionRespNode.builder().build();
        respNode.setId(0L);
        respNode.setTitle("默认顶级菜单");
        respNode.setSpread(true);
        respNode.setChildren(getTree(list,true));
        result.add(respNode);
        return result;
    }


    /**
     * 厂商端
     * @return
     */
    public List<PermissionRespNode> platformSelectAllByTree(Long userId) {
        List<SysPermission> list = selectAllPlatform(userId);
        return getTreePlatform(list,false);
    }

    /**
     * 平台端
     * @return
     */
    public List<PermissionRespNode> selectAllByTree() {
        List<SysPermission> list=selectAllping();
        return getTreePlatformAll(list,false);
    }

    public List<SysPermission> selectAllping() {
        List<SysPermission> result =MapperBeanUtils.getSysPermissionMapper().searchPingAll();
        if(!result.isEmpty()){
            for (SysPermission sysPermission:result){
                SysPermission parent=MapperBeanUtils.getSysPermissionMapper().selectByPrimaryKey(sysPermission.getPid());
                if (parent!=null){
                    sysPermission.setPidName(parent.getName());
                }
            }
        }
        return result;
    }


    public Set<String> getPermissionsByUserId(Long userId) {
        List<Long> roleIds = MapperBeanUtils.getSysUserRoleMapper().getRoleIdsByUserId(userId);
        if(roleIds.isEmpty()){
            return null;
        }
        List<Long> permissionIds = MapperBeanUtils.getSysRolePermissionMapper().getPermissionIdsByRoles(roleIds);
        if (permissionIds.isEmpty()){
            return null;
        }
        List<SysPermission> list = MapperBeanUtils.getSysPermissionMapper().selectInfoByIds(permissionIds);
        Set<String> permissions = new HashSet<>();
        if (null==list || list.isEmpty()){
            return null;
        }
        for (SysPermission sysPermission:list){
            if(!StringUtils.isEmpty(sysPermission.getPerms())){
                permissions.add(sysPermission.getPerms());
            }
        }
        return permissions;
    }

}
